/*!
 * \file frstr_append.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frstr.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    const char *p_append_val = '\0';

    p_frstr = frstr_new(0);
    i_ret = frstr_append(p_frstr, p_append_val, 0);



    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,0);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,1);
    CU_ASSERT_STRING_EQUAL(p_frstr->p_str,"");

    frstr_free(&p_frstr);

}

static void __case2(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    int i_size = 0;
    FILE *fp = NULL;
    char *p_append_val = NULL;

    p_frstr = frstr_new(0);

    fp = fopen("t_frstr/t_frstr_append/test1.txt","r");
    fseek(fp,0,SEEK_SET);
    fseek(fp,0,SEEK_END);

    i_size = ftell(fp) + 1;

    rewind(fp);

    p_append_val = (char *)frmalloc(i_size);

    if(p_append_val == NULL)
    {
        fclose(fp);
        frstr_free(&p_frstr);
        return;
    }
    memset(p_append_val,0x00,i_size);

    fread(p_append_val,1,i_size - 1,fp);
    i_ret = frstr_append(p_frstr, p_append_val, i_size - 1);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,2048);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,4097);
    CU_ASSERT_STRING_EQUAL(p_frstr->p_str,p_append_val);

    frfree(p_append_val);
    frstr_free(&p_frstr);
    fclose(fp);
}

static void __case3(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    int i_size = 0;
    FILE *fp = NULL;
    char *p_append_val = NULL;

    p_frstr = frstr_new(0);

    fp = fopen("t_frstr/t_frstr_append/test2.txt","r");
    fseek(fp,0,SEEK_SET);
    fseek(fp,0,SEEK_END);

    i_size = ftell(fp) + 1;

    rewind(fp);

    p_append_val = (char *)frmalloc(i_size);

    if(p_append_val == NULL)
    {
        fclose(fp);
        frstr_free(&p_frstr);
        return;
    }

    fread(p_append_val,1,i_size - 1,fp);
    i_ret = frstr_append(p_frstr, p_append_val, i_size - 1);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,2049);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,4098);
    CU_ASSERT_NSTRING_EQUAL(p_frstr->p_str,p_append_val,p_frstr->ui_len);


    frfree(p_append_val);
    frstr_free(&p_frstr);
    fclose(fp);
}

static void __case4(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    char *p_append_val = (char *)"\0";

    p_frstr = frstr_new(4);

    snprintf(p_frstr->p_str,5,"%s","name");
    p_frstr->ui_len = 4;

    i_ret = frstr_append(p_frstr, p_append_val, 0);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,4);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,9);
    CU_ASSERT_NSTRING_EQUAL(p_frstr->p_str,"name",p_frstr->ui_len);


    frstr_free(&p_frstr);

}

static void __case5(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    char *p_append_val = (char *)"john";

    p_frstr = frstr_new(4);

    snprintf(p_frstr->p_str,5,"%s","name");
    p_frstr->ui_len = 4;

    i_ret = frstr_append(p_frstr, p_append_val, 4);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,8);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,9);
    CU_ASSERT_NSTRING_EQUAL(p_frstr->p_str,"namejohn",p_frstr->ui_len);


    frstr_free(&p_frstr);

}

static void __case6(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    char *p_append_val = NULL;
    unsigned int ui_append_size = 104857600;

    p_frstr = frstr_new(0);

    p_append_val = (char *)frmalloc(104857600);
    memset(p_append_val,'\x08',104857600);

    i_ret = frstr_append(p_frstr, p_append_val, ui_append_size);

    CU_ASSERT_EQUAL(i_ret,2);
    CU_ASSERT_EQUAL(p_frstr->ui_len,0);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,1);

    frfree(p_append_val);
    frstr_free(&p_frstr);

}


static void __case7(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    char *p_append_val = NULL;
    unsigned int ui_append_size = 104857599;

    p_frstr = frstr_new(0);

    p_append_val = (char *)frmalloc(104857599);
    memset(p_append_val,'\x08',104857599);

    i_ret = frstr_append(p_frstr, p_append_val, ui_append_size);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,104857599);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,104859648);

    frfree(p_append_val);
    frstr_free(&p_frstr);

}



static void __case8(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    const char *p_append_val = "john";
    char *p_tar = NULL;

    p_frstr = frstr_new(9);
    memcpy(p_frstr->p_str,"name name",9);
    p_frstr->p_str[4] = '\0';
    p_frstr->ui_len = 9;

    p_tar = (char *)frmalloc(14);
    memcpy(p_tar,"name namejohn",13);
    p_tar[4] = '\0';

    i_ret = frstr_append(p_frstr, p_append_val, 4);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,13);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,19);
    CU_ASSERT_TRUE(memcmp(p_frstr->p_str,p_tar,13) == 0);

    frstr_free(&p_frstr);
    frfree(p_tar);

}


static void __case9(void)
{
    Frstr *p_frstr = NULL;
    int i_ret = 0;
    char *p_append_val = NULL;
    char *p_tar = NULL;

    p_frstr = frstr_new(4);
    memcpy(p_frstr->p_str,"name",4);
    p_frstr->ui_len = 4;

    p_tar = (char *)frmalloc(8);
    memcpy(p_tar,"namejohn",8);
    p_tar[6] = '\0';

    p_append_val = (char *)frmalloc(4);
    memcpy(p_append_val,"john",4);
    p_append_val[2] = '\0';

    i_ret = frstr_append(p_frstr, p_append_val, 4);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_EQUAL(p_frstr->ui_len,8);
    CU_ASSERT_EQUAL(p_frstr->ui_allocated_size,9);
    CU_ASSERT_TRUE(memcmp(p_frstr->p_str,p_tar,8) == 0);

    frstr_free(&p_frstr);
    frfree(p_tar);
    frfree(p_append_val);

}

int add_suite_frstr_append(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frstr frstr_append()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
    CU_add_test(pSuite, "case7", __case7);
    CU_add_test(pSuite, "case8", __case8);
    CU_add_test(pSuite, "case9", __case9);

    return FR_SUCC;
}
